package com.oceansoft.mobile.econsole.modules.jlapi.entity;

import com.google.common.base.Splitter;
import com.google.common.collect.Lists;
import com.oceansoft.mobile.econsole.modules.news.entity.News;

import java.util.List;

/**
 * ClassName: WeChatMediaMsg
 * Desc: 微信群发图文消息
 * Date： 2016/12/30
 * Created：shaom
 */
public class WeChatMediaMsg extends ApiMessage {

    private String openId;
    private String accessToken;
    private MsgType msgType;
    private List<PubMsg> articles;//图文消息，一个图文消息支持1到8条图文

    private List<PubMsg> customMsg;//客服消息

    public WeChatMediaMsg() {
        super(PushChannel.WECHATMEDIA);
    }

    public static WeChatMediaMsg build() {
        return new WeChatMediaMsg();
    }

    public String getOpenId() {
        return openId;
    }

    public WeChatMediaMsg setOpenId(String openId) {
        this.openId = openId;
        return this;
    }

    public String getAccessToken() {
        return accessToken;
    }

    public WeChatMediaMsg setAccessToken(String accessToken) {
        this.accessToken = accessToken;
        return this;
    }

    public List<PubMsg> getArticles() {
        return articles;
    }

    public WeChatMediaMsg setArticles(List<PubMsg> articles) {
        this.articles = articles;
        return this;
    }

    public WeChatMediaMsg setMsgType(MsgType msgType) {
        this.msgType = msgType;
        return this;
    }

    public MsgType getMsgType() {
        return msgType;
    }

    public List<PubMsg> getCustomMsg() {
        return customMsg;
    }

    public WeChatMediaMsg setCustomMsg(List<PubMsg> customMsg) {
        this.customMsg = customMsg;
        return this;
    }

    @Override
    public String toString() {
        return "WeChatMediaMsg{" +
                "openId='" + openId + '\'' +
                ", accessToken='" + accessToken + '\'' +
                ", articles=" + articles +
                '}';
    }

    /**
     * 构建图文发送的数据
     *
     * @param newsList
     * @return
     */
    public WeChatMediaMsg createMediaByNews(List<News> newsList) {
        List<PubMsg> pubMsgs = PubMsg.build().createMediaByNews(newsList);
        this.setArticles(pubMsgs);
        return this;
    }


    public static class PubMsg {
        // 图文消息
        private String thumb_media_id;//图文消息缩略图的media_id，可以在基础支持-上传多媒体文件接口中获得
        private String author;//图文消息的作者
        private String title;//图文消息的标题
        private String content_source_url;//在图文消息页面点击“阅读原文”后的页面
        private String content;//图文消息页面的内容，支持HTML标签。具备微信支付权限的公众号，可以使用a标签，其他公众号不能使用
        private String digest;//图文消息的描述
        private int show_cover_pic;//是否显示封面，1为显示，0为不显示

        // 客服消息
        private String description;
        private String url;
        private String picurl;

        public static PubMsg build() {
            return new PubMsg();
        }

        public List<PubMsg> createMediaByNews(List<News> newsList) {
            List<PubMsg> pubMsgs = Lists.newArrayList();
            for (News news : newsList) {
                PubMsg pubMsg = build()
                        .setThumb_media_id("/data/api.jl.gov.cn/upload/news/" + Splitter.on("/").splitToList(news.getImgUrl()).get(6))//需要和微信上传并获取
                        .setAuthor(news.getAuthor())
                        .setTitle(news.getTitle())
                        .setContent_source_url(news.getOrginalUrl())
                        .setContent(news.getContent())
                        .setDigest(news.getTitle())
                        .setShow_cover_pic(1);
                pubMsgs.add(pubMsg);
            }
            return pubMsgs;
        }

        public String getThumb_media_id() {
            return thumb_media_id;
        }

        public PubMsg setThumb_media_id(String thumb_media_id) {
            this.thumb_media_id = thumb_media_id;
            return this;
        }

        public String getAuthor() {
            return author;
        }

        public PubMsg setAuthor(String author) {
            this.author = author;
            return this;
        }

        public String getTitle() {
            return title;
        }

        public PubMsg setTitle(String title) {
            this.title = title;
            return this;
        }

        public String getContent_source_url() {
            return content_source_url;
        }

        public PubMsg setContent_source_url(String content_source_url) {
            this.content_source_url = content_source_url;
            return this;
        }

        public String getContent() {
            return content;
        }

        public PubMsg setContent(String content) {
            this.content = content;
            return this;
        }

        public String getDigest() {
            return digest;
        }

        public PubMsg setDigest(String digest) {
            this.digest = digest;
            return this;
        }

        public int getShow_cover_pic() {
            return show_cover_pic;
        }

        public PubMsg setShow_cover_pic(int show_cover_pic) {
            this.show_cover_pic = show_cover_pic;
            return this;
        }

        public String getDescription() {
            return description;
        }

        public PubMsg setDescription(String description) {
            this.description = description;
            return this;
        }

        public String getUrl() {
            return url;
        }

        public PubMsg setUrl(String url) {
            this.url = url;
            return this;
        }

        public String getPicurl() {
            return picurl;
        }

        public PubMsg setPicurl(String picurl) {
            this.picurl = picurl;
            return this;
        }

        @Override
        public String toString() {
            return "PubMsg{" +
                    "thumb_media_id='" + thumb_media_id + '\'' +
                    ", author='" + author + '\'' +
                    ", title='" + title + '\'' +
                    ", content_source_url='" + content_source_url + '\'' +
                    ", content='" + content + '\'' +
                    ", digest='" + digest + '\'' +
                    ", show_cover_pic=" + show_cover_pic +
                    '}';
        }
    }

    public enum MsgType {
        MEDIA, CUSTOM
    }
}
